<template>
  <el-dialog :visible.sync="dialogAddVisible"
             append-to-body
             width="40%"
             :close-on-click-modal="false"
             title="检样领检">
    <div class="csstop">
        <div class="cssborder"></div>
        <div class="cssborder2"></div>
        <div class="cssborder3"></div>
    </div>
    <el-divider></el-divider>
    <el-row>
      <el-col :span="12">
        <div class="el-dialog-item"><label>样品规格<i class="i_colon">：</i></label><span>{{detailData.materialType}}</span></div>
      </el-col>
      <el-col :span="12">
        <div class="el-dialog-item"><label class="w3">检验号</label><i class="i_colon">：</i><span style="color: #cb0000;font-size: 14px;">{{detailData.sampleId}}</span></div>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12">
        <div class="el-dialog-item"><label>物料编码<i class="i_colon">：</i></label><span>{{detailData.materialCode}}</span></div>
      </el-col>
      <el-col :span="12">
        <div class="el-dialog-item"><label class="w3">申请人</label><i class="i_colon">：</i><span>{{detailData.userName}}</span></div>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12">
        <div class="el-dialog-item"><label>样品名称<i class="i_colon">：</i></label><span>{{detailData.materialName}}</span></div>
      </el-col>
      <el-col :span="12">
        <div class="el-dialog-item"><label>申请时间<i class="i_colon">：</i></label><span>{{detailData.createTimeFt}}</span></div>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12">
        <div class="el-dialog-item"><label>样品等级<i class="i_colon">：</i></label><span>{{detailData.materialGrade}}</span></div>
      </el-col>
      <el-col :span="12">
        <div class="el-dialog-item"><label>送样地点<i class="i_colon">：</i></label><span>{{detailData.location}}</span></div>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <div class="el-dialog-item"><label>样品规模<i class="i_colon">：</i></label><span>{{detailData.sampleType}}</span></div>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <div class="el-dialog-item"><label>检项数量<i class="i_colon">：</i></label><span>{{choiceSize}}</span></div>
      </el-col>
    </el-row>
    <div class="el-dialog-table">
      <drug-table  @getSelection="getSelection" :filterPage="false" :isMultipleSelection="true"
                   :tableData="tableData" :tableLoading="tableLoading"
                   :tableHeader="tableHeader"  >
      </drug-table>
    </div>

    <div slot="footer" class="dialog-footer">
      <el-button type="green" class="zll_dialog_bt" size="mini" @click="updateSampleItem">
        领 检
      </el-button>
    </div>
  </el-dialog>
</template>
<script>
  import drugTable from "@/components/table/index";
  import { formatDate } from '@/utils/formatDate'
  import {getToken} from '@/utils/auth' // 验权
  export default {
    name:"takeTask",
    data(){
      return{
        user:{},
        detailData: {
          materialType:"",
          sampleId:"",
          materialCode:"",
          userName:"",
          materialName:"",
          createTimeFt:"",
          materialGrade:"",
          location:"",
          sampleType:"",
          filePaths:[]
        },
        dialogAddVisible: false,
        count: 0,
        tableData: [],
        tableHeader:[],
        tableLoading:true,
        selectChoice:[],
        oldChoice:[],
        choiceSize:"",
        fileName: '',
        fileNameTitle:''
      }
    },
    components:{
      drugTable
    },
    mounted(){
      let self = this;
      let user = JSON.parse(getToken());
      self.user = user;
      self.$eventBus.$on("openTakeTaskDialog",function (editData,operateType) {
        self.dialogAddVisible = true;
        self.count = 0;
        self.type = operateType;
        self.selectChoice = [];
        self.detailData = editData;
        self.getSampleItems();

      })
    },
    methods:{
      async batchSampleEvent(rows){
        let self = this;
        for(let i = 0;i < rows.length ;i++){
           await self.updateSampleItemAsync(rows[i],i);
        }
        return rows;
      },

      updateSampleItemAsync(row,i){
        let self = this;
        return new Promise((resolve, reject) => {
          self.detailData = row;
          let containsSampleItemId = self.detailData.containsSampleItemId;
          let userId = self.$store.getters.userId;
          let oldChoice = [];
          self.$http({
            url: "/drug/sampleItem/querySampleItemSend",
            method: "post",
            params:{sampleCode:self.detailData.sampleId}
          }).then(resp => {
            if (resp.success) {
              let datas = [];
              for(let data of resp.result.items){
                if(containsSampleItemId.indexOf(data.id) > -1){
                  if(!data.testStaffId && !data.resultId){
                    datas.push(data);
                  }
                  if(data.testStaffId == userId){
                    oldChoice.push(data);
                  }
                }
              }
              let params = [];
              if(datas.length == 0 ){
                resolve();
              }else{
                for(let data of datas){
                  let p = {allCount:datas.length,checkStatus:"SA",sampleId:self.detailData.sampleId,id:data.id,testStaffId:self.$store.getters.userId,testStaffOrg:self.user.deptId};
                  params.push(p);
                }
                self.$http({
                  url: "/drug/sampleItem/updateSampleItem",
                  method: "post",
                  data: JSON.stringify(params),
                  dataType: 'json',
                  contentType: "application/json",
                }).then(resp => {
                  if (resp.success) {
                    self.generateRecord(self.detailData,datas,oldChoice);
                  }else{
                    row.submit = "error"
                  }
                  resolve();
                }).catch(() => {
                  row.submit = "error"
                  reject();
                });
              }
            }else{
              row.submit = "error"
              reject();
            }
          }).catch(() => {
            row.submit = "error"
            reject();
          });
        })
      },



      updateSampleItem(){
        let self = this;
        let params = [];
        if(self.selectChoice.length < 1){
          self.$notify({
            title: '提示',
            message: "请选择领检项！",
            type: 'warning'
          });
          return;
        }
        if(self.count !=0){
          return;
        }
        for(let data of self.selectChoice){
          let p = {allCount:self.tableData.length,checkStatus:"SA",sampleId:self.detailData.sampleId,id:data.id,testStaffId:self.$store.getters.userId,testStaffOrg:self.user.deptId};
          params.push(p);
        }
        self.$http({
          url: "/drug/sampleItem/updateSampleItem",
          method: "post",
          data: JSON.stringify(params),
          dataType: 'json',
          contentType: "application/json",
        }).then(resp => {
          if (resp.success) {
            self.dialogAddVisible = false;
            self.generateRecord(self.detailData,self.selectChoice,self.oldChoice);
            self.$eventBus.$emit("updateDetectionList");
            self.$notify({
              title: '提示',
              message: "领检成功！",
              type: 'success'
            });
            return
          }
          self.count--;
          self.$notify({
            title: '提示',
            message: "操作失败，请联系管理员！",
            type: 'error'
          });
        });
        this.count++;
      },

      //
      // generatePdfPath(sampleId){
      //   let self = this;
      //   self.$http({
      //     url: "/drug/sampleItem/queryExportSampleRecord",
      //     method: "post",
      //     params:{sampleCode:sampleId,userId:self.$store.getters.userId}
      //   }).then(resp => {
      //     if (resp.success) {
      //       self.generateRecord(resp.result[0]);
      //     }
      //   });
      // },

      getCell(text,index,backGround,width){
        let cell = {
          text:text,
          fontSize:11,
          family:"黑体",
          index:index?index:0,
          width:width?width:0
        };
        if(backGround){
          cell.backGround = "EEEEEE"
        }
        return cell;
      },

      getCellCode(text){
        let cell = {
          code:text,
          index:0
        };
        return cell;
      },

      getQuCell(text,index){
        let cell = {
          text:text,
          fontSize:10,
          family:"黑体",
          splitStr:44,
          index:index?index:0,
          position:'left',
          width:0
        };
        return cell;
      },

      generateRecord(row,standardItems,oldChoice){
        let self = this;
        let cellList = [];
        cellList.push(this.getCell("物料编码",0,"EEEEEE",1500));
        cellList.push(this.getCell(row.materialCode,0,""));
        cellList.push(this.getCell("检 验 号",0,"EEEEEE",1500));
        cellList.push(this.getCell(row.sampleId,0));
        cellList.push(this.getCellCode(row.sampleId,0,"",2000));

        cellList.push(this.getCell("样品名称",1,"EEEEEE",1500));
        cellList.push(this.getCell(row.materialName,1,""));


        cellList.push(this.getCell("样品规格",2,"EEEEEE",1500));
        cellList.push(this.getCell(row.materialType,2,""));
        cellList.push(this.getCell("样品规模",2,"EEEEEE",1500));
        cellList.push(this.getCell(row.sampleType,2));

        cellList.push(this.getCell("样品等级",3,"EEEEEE",1500));
        cellList.push(this.getCell(row.materialGrade,3,""));
        cellList.push(this.getCell("申 请 人",3,"EEEEEE",1500));
        cellList.push(this.getCell(row.userName,3));
        cellList.push(this.getCell("样品批号",4,"EEEEEE",1500));
        cellList.push(this.getCell(row.sampleNum,4,""));
        cellList.push(this.getCell("申请时间",4,"EEEEEE",1500));
        cellList.push(this.getCell(row.createTimeFt,4));
        let splitRow = [];
        splitRow.push("4,3,4,0");
        splitRow.push("2,0,1,1");
        splitRow.push("3,0,1,1");
        splitRow.push("4,0,3,1");
        let day =  formatDate(new Date(), "yyyy-MM-dd hh:mm:ss");
        // let standardItems = row.standardItems;
        oldChoice.push(...standardItems);
        let uname = self.$store.getters.userName;
        let param ={
          id:row.id,
          userId:self.$store.getters.userId,
          sampleCode:row.sampleId,
          title:"分析测试中心检验原始记录",
          printPerson:"打印人："+uname +" "+day,
          graphWordList:[
            {
              title:"检样信息：",
              colIndex:5,
              cellList:[],
              splitRow:[]
            },
            {
              remark:"检验人:"+this.$store.getters.userName,
              title:"检项：( "+oldChoice.length+"/"+row.totalCount+" )",
              colIndex:5,
              cellList:[],
              splitRow:[],
              image:'E:\\zhang.png'
            },
            {
              title:"检验方法：",
              colIndex:4,
              position:'left',
              cellList:[],
              splitRow:[]
            }
          ]
        };
        param.graphWordList[0].cellList = cellList;
        param.graphWordList[0].splitRow = splitRow;

        let cellList1 = [];
        cellList1.push(this.getCell("检项",0,"EEEEEE",1200));
        cellList1.push(this.getCell("质量标准",0,"EEEEEE"));
        cellList1.push(this.getCell("检验结果",0,"EEEEEE",4000));
        cellList1.push(this.getCell("备注",0,"EEEEEE",1000));
        cellList1.push(this.getCell("工时",0,"EEEEEE",800));
        if(oldChoice && oldChoice.length  > 0){
          for(let i = 0 ; i < oldChoice.length ;i++){
            cellList1.push(this.getCell(oldChoice[i].itemName,i+1));
            cellList1.push(this.getQuCell(oldChoice[i].itemQualityStandard,i+1));
            cellList1.push(this.getCell(oldChoice[i].testResult,i+1));
            cellList1.push(this.getCell(oldChoice[i].remark,i+1));
            cellList1.push(this.getCell(oldChoice[i].manHour,i+1));
          }
        }
        param.graphWordList[1].cellList = cellList1;
        let cellList12 = [];
        let splitRow1 = [];
        if(oldChoice && oldChoice.length  > 0){
          for(let k = 0 ; k < oldChoice.length ;k++){
            let methodDesc = oldChoice[k].methodDesc?oldChoice[k].methodDesc:"无方法描述";
            let testRecord = oldChoice[k].testRecord?oldChoice[k].testRecord:"无测试记录";
            cellList12.push(this.getCell(methodDesc,k+8*k));
            cellList12.push(this.getCell("",k+8*k));
            cellList12.push(this.getCell(testRecord,k+8*k,"",3000));
            cellList12.push(this.getCell("",k+8*k,"",3000));
            for(let i = 1;i<9;i++){
              cellList12.push(this.getCell("",i+9*k));
              cellList12.push(this.getCell("",i+9*k));
              cellList12.push(this.getCell("",i+9*k));
              cellList12.push(this.getCell("",i+9*k));
            }

            splitRow1.push("0,"+(9*k)+","+(9*k+8)+",1");
            splitRow1.push(9*k+",0,1,0");
            splitRow1.push(9*k+",2,3,0");
            splitRow1.push("1,"+(9*k)+","+(9*k+8)+",1");
            splitRow1.push((9*k+1)+",0,1,0");
            splitRow1.push((9*k+1)+",2,3,0");
            splitRow1.push("2,"+(9*k)+","+(9*k+8)+",1");
            splitRow1.push((9*k+2)+",0,1,0");
            splitRow1.push((9*k+2)+",2,3,0");
            splitRow1.push("3,"+(9*k)+","+(9*k+8)+",1");
            splitRow1.push((9*k+3)+",0,1,0");
            splitRow1.push((9*k+3)+",2,3,0");
            splitRow1.push((9*k+4)+",0,1,0");
            splitRow1.push((9*k+4)+",2,3,0");
            splitRow1.push((9*k+5)+",0,1,0");
            splitRow1.push((9*k+5)+",2,3,0");
            splitRow1.push((9*k+6)+",0,1,0");
            splitRow1.push((9*k+6)+",2,3,0");
            splitRow1.push((9*k+7)+",0,1,0");
            splitRow1.push((9*k+7)+",2,3,0");
            splitRow1.push((9*k+8)+",0,1,0");
            splitRow1.push((9*k+8)+",2,3,0");
          }
          splitRow1.push((9*(oldChoice.length)+1)+",1,3,0");
          cellList12.push(this.getCell("检验人",9*(oldChoice.length)));
          cellList12.push(this.getCell(".    .",9*(oldChoice.length)));

          cellList12.push(this.getCell("检验时间",9*(oldChoice.length),"",2200));
          cellList12.push(this.getCell(" 年  月  日 ",9*(oldChoice.length),"",2300));

          cellList12.push(this.getCell("说  明",9*(oldChoice.length)+1));
          cellList12.push(this.getCell(row.remark,9*(oldChoice.length)+1));
          cellList12.push(this.getCell("",9*(oldChoice.length)+1));
          cellList12.push(this.getCell("",9*(oldChoice.length)+1));
        }

        param.graphWordList[2].cellList = cellList12;
        param.graphWordList[2].splitRow = splitRow1;

        self.$http({
          url: "/drug/sample/queryIframePath",
          method: "post",
          data: JSON.stringify(param),
          dataType: 'json',
          contentType: "application/json",
        }).then(resp => {
          if (resp.success) {
            // self.showPdf(resp.result);
          }
        });
      },

      openPrintView(){
        let time = (new Date()).getTime();
        let routeData = this.$router.resolve({ path: "/iframe?code="+this.detailData.sampleId+"&&time="+time });
        window.open(routeData.href, '_blank');
      },

      getSelection(val){
        this.selectChoice = val;
        this.choiceSize = val.length+"/"+this.tableData.length
      },
      getSampleItems(){
        let self = this;
        self.tableLoading = true;
        let containsSampleItemId = self.detailData.containsSampleItemId;
        let userId = self.$store.getters.userId;
        self.oldChoice = [];
        self.$http({
          url: "/drug/sampleItem/querySampleItemSend",
          method: "post",
          params:{sampleCode:self.detailData.sampleId}
        }).then(resp => {
          if (resp.success) {
            self.tableLoading = false;
            let datas = [];
            for(let data of resp.result.items){
              if(containsSampleItemId.indexOf(data.id) > -1){
                if(!data.testStaffId && !data.resultId){
                  datas.push(data);
                }
                if(data.testStaffId == userId){
                  self.oldChoice.push(data);
                }
              }
            }
            self.tableData = datas;
            self.tableHeader =  [
              {"columnName": "itemName", "coloumNameCn": "检项名称"},
              {"columnName": "itemQualityStandard", "coloumNameCn": "质量标准"}];
            self.choiceSize = "0/"+self.tableData.length
          }
        });
      },
      uploadFileSample(event){
        let self = this;
        let fileList = event.target.files;
        if (fileList.length > 0) {
          self.detailData.filePaths = [];
          let fileNameTitle = '';
          let fileNameClass = '';
          for(let i = 0; i < fileList.length;i++){
            let file = fileList[i];
            fileNameTitle +=  file.name+','
            fileNameClass += `<p class="list" style="margin: 0; line-height: 18px;">`
                            + `<span class="nameaa" ref="nameaa">` + file.name + `</span>`
                            + `<span class="el-icon-circle-close closeFile" style="cursor: pointer;" ></span></p>`
            let fileObj = {fileName:file.name,fileBlob:"",id:self.detailData.sampleId,fileType:'code'};
            if (/.(jpg|jpeg|png|gif|pdf|bmp)$/.test(file.name)) {
              let reader = new FileReader();
              reader.readAsDataURL(file);
              reader.onload = function (e) {
                fileObj.fileBlob = e.currentTarget.result;
                self.detailData.filePaths.push(fileObj);
                self.fileNameTitle = fileNameTitle;
              	self.fileName = self.detailData.filePaths.length + '个文件'
                self.$refs.fileTarget.innerHTML = fileNameClass;
              	let $deleteDom = document.getElementsByClassName('list');
              	for(let $dom of $deleteDom){
                  $dom.addEventListener('click', function () {
                    self.clickT($dom)
                  })
                }
              };
            } else {
              self.$notify({
                title: '提示',
                message: "上传文件必须是图片或pdf或压缩包！",
                type: 'warning'
              });
              self.detailData.filePaths = [];
              self.fileNameTitle = '';
              self.fileName = '0个文件'
              let obj = document.getElementById('sampleFile') ;
              obj.outerHTML = obj.outerHTML;
              self.$refs.fileTarget.innerHTML = '';
              break;
            }
          }
        }else{
          self.detailData.filePaths= [];
          self.fileName = '0个文件'
          self.fileNameTitle = '';
          self.$refs.fileTarget.innerHTML = '';
        }
      },
      clickT(data){
        let f = document.getElementById('fileTarget');
        let str = this.fileNameTitle.split(',').slice(0,-1)
        for(var i = 0; i < str.length; i++) {
            if(data.innerHTML.indexOf(str[i]) !== -1) {
                let deleteName = f.childNodes;
                deleteName[i].parentNode.removeChild(deleteName[i]);
                // f.appendChild(deleteName)
                this.fileName = deleteName.length + '个文件'
            }
        }
　　　 }
    }
  }
</script>
<style lang="scss" scoped>


</style>
